package com.drug.lib.view.recycptr;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.drug.lib.R;
import com.drug.lib.view.recycptr.bean.BaseEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2022/4/15 or 17:50
 * 描述：---
 */
public class RefreshRecyclerView extends LinearLayout {
    private RefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private FrameLayout container;
    private Context context;
    private int page = 1;
    private int size = 20;
    private DynamicAdpTypePool pool = new DynamicAdpTypePool();
    private DynamicRecyclerAdapter dynamicRecyclerAdapter;

    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.ItemDecoration itemDecoration;
    private RecyclerView.OnScrollListener onScrollListener;

    private OnRefreshLoadMoreListener onRefreshLoadMoreListener;
    private OnItemClickListener onItemClickListener;

    private boolean isCanRefresh = true;
    private boolean isCanLoadMore = true;
    private boolean isEnablePureScrollMode = false;
    private boolean isEnableNestedScroll = false;
    private List<BaseEntity> listData = new ArrayList<>();

    public RefreshRecyclerView(Context context) {
        super(context);
        init(context, null);
    }

    public RefreshRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RefreshRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        View layout = LayoutInflater.from(context).inflate(R.layout.recyclerview_common_layout, this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RefreshRecyclerView);
        this.context = context;
        recyclerView = (RecyclerView) layout.findViewById(R.id.recyclerView);
        refreshLayout = layout.findViewById(R.id.refreshLayout);
        container = layout.findViewById(R.id.ll_container);
    }


    public void initView() {
        setCanRefresh(isCanRefresh);
        setCanLoadMore(isCanLoadMore);
        setEnablePureScrollMode(isEnablePureScrollMode);
        setEnableNestedScroll(isEnableNestedScroll);
    }

    /**
     * 初始化RecyclerView
     */
    public void initRecyclerView() {
        if (layoutManager == null)
            layoutManager = new LinearLayoutManager(context);
        setLayoutManager(layoutManager);
        setOnScrollListener(onScrollListener);
        addItemDecoration(itemDecoration);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        dynamicRecyclerAdapter = new DynamicRecyclerAdapter();
        dynamicRecyclerAdapter.setItems(listData);

    }


    public <T> void register(Class<? extends T> clazz, ItemViewBinder<T, ?> binder) {
        pool.register(clazz, binder);
    }

    public void setAdapter() {
        setAdapter(null);
    }

    public void setAdapter(List<BaseEntity> baseEntities) {
        if (baseEntities != null) listData.addAll(baseEntities);
        dynamicRecyclerAdapter.registerAll(pool);
        recyclerView.setAdapter(dynamicRecyclerAdapter);
    }


    public void setCanRefresh(boolean canRefresh) {
        this.isCanRefresh = canRefresh;
        refreshLayout.setEnableRefresh(isCanRefresh);
    }

    public void setCanLoadMore(boolean canLoadMore) {
        this.isCanLoadMore = canLoadMore;
        refreshLayout.setEnableLoadMore(isCanLoadMore);
    }

    public void setEnablePureScrollMode(boolean canPureScrollMode) {
        this.isCanRefresh = !canPureScrollMode;
        this.isCanLoadMore = !canPureScrollMode;
        this.isEnablePureScrollMode = canPureScrollMode;
        setCanLoadMore(isCanLoadMore);
        setCanRefresh(isCanRefresh);
        refreshLayout.setEnablePureScrollMode(isEnablePureScrollMode);
    }

    public void setEnableNestedScroll(boolean enableNestedScroll) {
        this.isEnableNestedScroll = enableNestedScroll;
        refreshLayout.setEnableNestedScroll(isEnableNestedScroll);
    }

    public void setRecyclerLoadListener(OnRefreshLoadMoreListener onRefreshLoadMoreListener) {
        if (onRefreshLoadMoreListener != null) {
            this.onRefreshLoadMoreListener = onRefreshLoadMoreListener;
            refreshLayout.setOnRefreshLoadMoreListener(this.onRefreshLoadMoreListener);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        if (onItemClickListener != null)
            dynamicRecyclerAdapter.setOnItemClickListener(this.onItemClickListener);
    }

    public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
        if (onScrollListener != null) {
            this.onScrollListener = onScrollListener;
            recyclerView.addOnScrollListener(this.onScrollListener);
        }
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        if (layoutManager != null) {
            this.layoutManager = layoutManager;
            recyclerView.setLayoutManager(this.layoutManager);
        }
    }

    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        if (itemDecoration != null) {
            this.itemDecoration = itemDecoration;
            recyclerView.addItemDecoration(this.itemDecoration);
        }
    }


    public DynamicRecyclerAdapter getDynamicRecyclerAdapter() {
        return dynamicRecyclerAdapter;
    }

    public void autoRefreshData() {
        refreshLayout.autoRefresh();
    }


    public FrameLayout getContainer() {
        return container;
    }

    public void showContainer() {
        refreshLayout.getLayout().setVisibility(GONE);
        container.setVisibility(VISIBLE);
    }

    public void hideContainer() {
        refreshLayout.getLayout().setVisibility(VISIBLE);
        container.setVisibility(GONE);
    }

    public void refreshComplete(boolean success) {
        refreshLayout.finishRefresh(success);
    }

    public void loadMoreComplete(boolean success) {
        refreshLayout.finishLoadMore(success);
    }

    public void loadMoreNoMoreData() {
        refreshLayout.finishLoadMoreWithNoMoreData();
    }

    public void setNoMoreData(boolean success) {
        refreshLayout.setNoMoreData(success);
    }


    public void addDataToList(List<? extends BaseEntity> list) {
        addDataToList(list, -1);
    }

    public void addDataToList(List<? extends BaseEntity> list, int position) {
        if (list != null) {
            if (position == -1) {
                this.listData.addAll(list);
            } else {
                this.listData.addAll(position, list);
            }
        }
        dynamicRecyclerAdapter.notifyDataSetChanged();
    }

    public void addDataToList(BaseEntity baseEntity, int position) {
        if (baseEntity != null) {
            if (position == -1) {
                this.listData.add(baseEntity);
            } else {
                this.listData.add(position, baseEntity);
            }
        }
        dynamicRecyclerAdapter.notifyDataSetChanged();
    }

    public void refreshData(List<? extends BaseEntity> list) {
        this.listData.clear();
        this.listData.addAll(list);
        dynamicRecyclerAdapter.notifyDataSetChanged();
        if (isCanRefresh) refreshLayout.finishRefresh();

    }

    public void loadMoreData(List<? extends BaseEntity> list) {
        listData.addAll(list);
        dynamicRecyclerAdapter.notifyDataSetChanged();
        if (isCanLoadMore) refreshLayout.finishLoadMore(1000);
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public List<BaseEntity> getListData() {
        return listData;
    }
}
